package com.ndp.fb.rdb.mongo.impl;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.ReflectionDBObject;
import com.mongodb.WriteResult;
import com.ndp.fb.mongo.api.BaseMongoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by jacky.cheng on 2015/9/28.
 */
public abstract class BaseMongoServiceImpl implements BaseMongoService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    protected DB mongoDBClient;
    @Override
    public  void save(ReflectionDBObject o) {
        if (o == null) {
            logger.error("param is null ,not save!");
            return;
        }
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        dbCollection.insert(o);

    }

    @Override
    public  void save(DBObject o) {
        if (o == null) {
            logger.error("param is null ,not save!");
            return;
        }
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        dbCollection.insert(o);

    }
    @Override
    public void save(JSONObject o){
        if (o != null) {
            save(o.toJSONString());
        }else {
            logger.error("param is null ,not save!");
        }
    }
    @Override
    public void save(String json) {
        DBObject o;
        if (StringUtils.hasText(json)) {
            o = (DBObject) com.mongodb.util.JSON.parse(json);
            if (o != null) {
                save(o);
            }else {
                logger.error("parse jsonObject failed.");
            }
        } else {
            logger.error("param is null ,not save!");
        }
    }
    @Override
    public void saveAllJson(List<JSONObject> os){
        List<DBObject> ds = (List<DBObject>) com.mongodb.util.JSON.parse(JSONObject.toJSONString(os));
        saveAllDb(ds);
    }

    @Override
    public Iterator<DBObject> find(DBObject q){
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBCursor cursor = dbCollection.find(q);
        return cursor.iterator();
    }

    @Override
    public Boolean updates(DBObject q, DBObject fs) {
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        WriteResult update = dbCollection.update(q, fs);
        return update.isUpdateOfExisting();
    }

    @Override
    public void saveAll(List<ReflectionDBObject> os) {
        if (os == null || os.size() < 1) {
            return;
        }
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        List<DBObject> data = new ArrayList<>();
        data.addAll(os);
        dbCollection.insert(data);
    }

    public void saveAllDb(List<DBObject> os){
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        dbCollection.insert(os);
    }

    public abstract String getTableName();
}
